package KK.Controls.Button
{
	import KK.Controls.Skin.ButtonSheet;
	import KK.Controls.Skin.ISkinSheet;
	import KK.System.System;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BaseButton extends Sprite
	{
		private static const MAX_WIDTH:int=3;
		private var WIDTH_PIXEL:int;
		private var HEIGHT_PIXEL:int;
		private var source:BitmapData;
		private var sheet:ISkinSheet;
		
		private var skin:Bitmap;
		
		private var size:Point;
		private var desPoint:Point;
		public function BaseButton()
		{ 
			super();
			
			init();
		}
		/**
		 * 初始化  
		 * 
		 */
		private function init():void{
			initEvent();
			desPoint=new Point();
			size=new Point;
			skin=new Bitmap();
			addChild(skin); 
		}
		
		/**
		 * 设置皮肤  
		 * @param value
		 * 
		 */
		public function set Skin(value:BitmapData):void{
			source=value;
			WIDTH_PIXEL=value.width/MAX_WIDTH;
			HEIGHT_PIXEL=value.height;
			sheet=new ButtonSheet(value.width/MAX_WIDTH,value.height);
			setSize(size.x,size.y);
			Out();
		}
		
		/**
		 * 设置大小  
		 * @param width
		 * @param height
		 * 
		 */
		public function setSize(width:int,height:int):void{
			size.x=width>WIDTH_PIXEL?width:WIDTH_PIXEL;
			size.y=HEIGHT_PIXEL;
			if(isSkin()){
				skin.bitmapData&&skin.bitmapData.dispose();
				skin.bitmapData=new BitmapData(size.x,size.y,true,0);
				sheetResize();
			}
		}
		
		
		/**
		 * 离开  
		 * 
		 */
		public function Out():void{
			desPoint.y=desPoint.x=0;
			draw();
		}
		
		/**
		 * 鼠标按下  
		 * 
		 */
		public function Down():void{
			desPoint.x=WIDTH_PIXEL*2;
			desPoint.y=0;
			draw();
		}
		
		/**
		 * 鼠标经过   
		 * 
		 */
		public function Over():void{
			desPoint.x=WIDTH_PIXEL;
			desPoint.y=0;
			draw();
		}
		
		/**
		 *  是否已经设置皮肤 
		 * @return 
		 * 
		 */
		public function isSkin():Boolean{
			return source!=null;
		}
		 
		/**
		 * 绘制  
		 * 
		 */
		private function draw():void{
			sheet.setOffset(desPoint);
			skin.bitmapData.fillRect(new Rectangle(0,0,size.x,size.y),0); 

			System.FillSheetSkin(source,skin.bitmapData,sheet);
			
		}
		
		/**
		 * 大小重置  
		 * 
		 */
		private function sheetResize():void{
			sheet.setSize(size,new Point());
			Out();
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void{
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseHandler);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseHandler);
			addEventListener(MouseEvent.MOUSE_UP,onMouseHandler);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseHandler);
		}
		
		/**
		 * 鼠标事件处理  
		 * @param event
		 * 
		 */
		private function onMouseHandler(event:MouseEvent):void{
			if(!skin.bitmapData){
				return ;
			}
			switch(event.type){
				case MouseEvent.MOUSE_DOWN:
					Down();
					break;
				case MouseEvent.MOUSE_UP: 
				case MouseEvent.MOUSE_OVER:
					Over();
					break;
				case MouseEvent.MOUSE_OUT:
					Out();
					break;
			}
		}
	}
}